package com.igeekhome.service.impl;

import com.igeekhome.mapper.SysStaffMapper;
import com.igeekhome.pojo.DNetwork;
import com.igeekhome.pojo.SysStaff;
import com.igeekhome.pojo.SysStaffExample;
import com.igeekhome.service.IStaffService;
import com.igeekhome.util.CommonUtil;
import com.igeekhome.util.DtreeNode;
import com.igeekhome.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 立以宁
 */
@Service("sysStaffService")
public class SysStaffServiceImpl implements IStaffService {

    @Autowired
    private SysStaffMapper sysStaffMapper;
    
    @Value("${DIST_CLERK}")
    private Integer DIST_CLERK;

    @Override
    public List<SysStaff> selectAll() {
        List<SysStaff> sysStaffs = sysStaffMapper.selectByExample(null);
        return sysStaffs;
    }

    @Override
    public SysStaff login(Integer loginAccount, String password) {

        // TODO Auto-generated method stub

        SysStaffExample example = new SysStaffExample();
        SysStaffExample.Criteria criteria = example.createCriteria();
            criteria.andLoginAccountEqualTo(loginAccount);

        List<SysStaff> sysStaffs = sysStaffMapper.selectByExample(example);

        //如果查询到的账号存在
        if (sysStaffs.size() != 0) {
            SysStaff staff = sysStaffs.get(0);
                if (CommonUtil.md5(password).equals(staff.getLoginPassword())) {
                    return staff;
                }

        }
        return null;
    }

    @Override
    public List<SysStaff> selectByIds(Integer rid, Integer nid) {
        HashMap<String,Integer> map = new HashMap<String,Integer>(2){
            {
                // 插入查询信息
                put("rid",rid);
                put("nid",nid);
            }
        };
        return sysStaffMapper.selectByRidAndNid(map);
    }
    
    
    /**
     *@author 汪璇
     * @Description 查配送员
    * @Param [network]
    * @Return java.util.List<com.igeekhome.pojo.SysStaff>
    **/
    @Override
    public ResultBean selectByNetworkId(DNetwork network) {
        Integer networkId = network.getNetworkId();
        List<SysStaff> staffs = sysStaffMapper.selectByNetworkId(networkId, DIST_CLERK);
        List<DtreeNode> nodes = new ArrayList<>();
        for (SysStaff staff : staffs) {
            DtreeNode node = new DtreeNode(staff.getStaffId().toString(),staff.getName());
            nodes.add(node);
        }
        return ResultBean.ok(nodes);
    }
    
}
